package com.day4.system.dao;

import com.day4.system.domain.Student;

/**
 * @author 小坡
 * @date 2022/8/20  12:13
 * 库管  用于数据的增删改查的
 **/
public class StudentDao extends BaseStudentDao{
    private static Student[] students = new Student[5];

    //    在这里使用代码块  进行数据的初始化  可以利用这个  来测试其他功能的实现 更方便  省略了每次运行的时候 需要每次录入信息
    static {
        Student student = new Student("heima01", "张三", "23", "1999-1-1");
        Student student2 = new Student("heima02", "李四", "23", "1999-1-1");
        students[0] = student;
        students[1] = student2;
    }

    @Override
    public boolean addStudent(Student stu) {

        int index = -1;
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("装满了，添加失败");
            return false;
        } else {
            System.out.println("添加成功");
            students[index] = stu;
            return true;
        }
    }

    public Student[] findAllStudent() {
        return students;
//        这里直接返回这个数组就可以了
    }

    public void delStudent(String delid) {
//        查找这个id在容器中所在的位置——在这个位置使用nulll进行覆盖——

        int index = getIndex(delid);
        students[index] = null;
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < students.length; i++) {
            Student stu = students[i];
            if (stu != null && stu.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String updateid, Student newstu) {
        int index = getIndex(updateid);
        students[index] = newstu;
    }
}
